using System;
using System.Data;
using System.Collections;

/*
    CssDom Library
    Copyright (C) 2007-2011  Ferenc Veres

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

namespace WPS.Libraries.CssDom
{
	/// <summary>
	/// The CSSStyleRule interface represents a single rule set in a CSS style sheet.
	/// </summary>
	public class CSSStyleRule : CSSRule
	{
		#region CSSStyleRule interface public properties

		/// <summary>
		/// The textual representation of the selector for the rule set. 
		/// Strips out insignificant whitespace while parsing the selector.
		/// </summary>
		public string selectorText
		{
			get
			{
				return m_selectorText;
			}
			set
			{
				m_selectorText = value;
			}
		}
		protected string m_selectorText;

		/// <summary>
		/// The declaration-block of this rule set.
		/// </summary>
		public CSSStyleDeclaration style
		{
			get
			{
				return m_style;
			}
			set
			{
				m_style = value;
			}
		}
		protected CSSStyleDeclaration m_style;

		#endregion

		#region Initialziation

		/// <summary>
		/// Creates an empty CSSStyleRule.
		/// </summary>
		public CSSStyleRule()
		{
			this.m_type = CSSRule.STYLE_RULE;
		}

		/// <summary>
		/// Creates a CSSStyleRule with initial cotnent.
		/// </summary>
		/// <param name="rule">Parsable cssText definition for a complete CSS Rule.</param>
		public CSSStyleRule(string rule)
		{
			this.m_type = CSSRule.STYLE_RULE;
			parseRule(rule);
		}
		#endregion

		#region Implementation related nonstandard functions

		/// <summary>
		/// cssText property uses this function to retrieve it's content.
		/// </summary>
		/// <returns>A parsable string of the current CSS Rule.</returns>
		public override string getCssText()
		{
			return this.selectorText + "\n{\n" + style.cssText + "}\n";
		}

		/// <summary>
		/// Sets the cssText property by parsing the passed complete CSS rule.
		/// </summary>
		/// <param name="rule">A parsable complete CSS Rule to store in the current object.</param>
		public override void setCssText(string rule)
		{
			parseRule(rule);
		}

		/// <summary>
		/// Parse the rule declaration and set to our properites (e.g. "h1 { font-size: 100%; }")
		/// </summary>
		/// <param name="rule"></param>
		private void parseRule(string rule)
		{
			// Remove closing tag, then Split removes opening one
			rule = rule.Replace("}", "");
			string[] styleDef = rule.Split('{');

			if(styleDef.GetLength(0) == 1)
			{
				// No definition, just name
				this.selectorText = styleDef[0].Trim();
			}
			else if(styleDef.GetLength(0) == 2)
			{
				// Name and definition, store the class
				this.selectorText = styleDef[0].Trim();
				this.style = new CSSStyleDeclaration(styleDef[1].Trim());
			}
		}
		#endregion

	}
}
